<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 15-9-23
 * Time: 下午4:34
 */
class QuotationModel extends BaseModel{
    private $_table;
    protected $_name = 'iq_quotation';
    protected $_isbug = false;

###################################################################################################
    /**
     * @param $id
     * @return array
     */
    public function getDetailsById($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id",array("service_name"))
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","business_phone","contacts","mobile","qq"))

            ->where ( $where )->__toString ();
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchRow ( $sql );
    }

    /**
     * @param $were
     * @return string
     */
    public function getQuoteCountByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,"count(*)" )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id","")
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id","")
            ->joinLeft ( "product", "$this->_name.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id","")
            ->joinLeft("region","region.region_id = $this->_name.area_id","")
            ->where ( $where )
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }

    public function getQuotesCountByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,"count(*)" )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id","")
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id","")
            ->joinLeft ( "product", "$this->_name.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id","")
            ->joinLeft("region","region.region_id = $this->_name.area_id","")
            ->where ( $where )
            ->__toString ();

        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        return $db->fetchOne( $sql );
    }


    /**
     * @param $where
     * @param $item_page
     * @param $offset
     * @param $order
     * @param $group
     * @return array
     */
    public function getQuoteListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id",array("service_name"))
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "users_invoice", "$this->_name.business_id = users_invoice.id",array ( "business_name","business_phone","contacts","mobile","qq"))
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name as area_name")
            ->where ( $where )
            ->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        $data = $db->fetchAll ( $sql );
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['update_time'] = Tools::time2Units(time() - strtotime($v['update_time'])) . "前";
            }
        }
        return $data;
    }

    public function getQuotesListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id",array("service_name"))
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "iq_inquiry", "iq_inquiry.id = $this->_name.inquiry_id", "business_id" )
            ->joinLeft ( "users_invoice", "iq_inquiry.business_id = users_invoice.id",array ( "business_name","business_phone","contacts","mobile","qq"))
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name as area_name")
            ->where ( $where )
            ->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }
        if ($this->_isbug == true) {
            echo __METHOD__ . ": " . $sql . "<hr/>";
        }
        $data = $db->fetchAll ( $sql );
        if($data){
            foreach($data as $k=>$v){
                $data[$k]['update_time'] = Tools::time2Units(time() - strtotime($v['update_time'])) . "前";
            }
        }
        return $data;
    }

    public function getDownList($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,array("price","num","settlement","delivery_area","delivery_time","delivery_method") )
            ->joinLeft ( "product", "$this->_name.product_id = product.id", array("product_name"))
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name" )
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "res_service","$this->_name.service_id = res_service.id",array("service_name"))
            ->joinLeft("users_invoice","$this->_name.business_id = users_invoice.id",array("business_name"))
            ->joinLeft ( "res_currency","$this->_name.currency_id = res_currency.id",array("mark as currency_mark","name as currency_name"))
            ->joinLeft("region","region.region_id = $this->_name.area_id","region_name")
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }

        $data = $db->fetchAll ( $sql );

        return $data;
    }

    //删除
    public function delquote($id){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        return $db->delete($this->_name,"id in ({$id})");
    }

    //根据id查询信息
    public function getQuoteStatusByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name,'status' )
            ->where($where)->__toString ();
        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchOne( $sql );
        return $data;
    }
    //这边查询对求购是否报价
    public function getQuoteByInquireId($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ( $this->_name)
            ->where($where)->__toString ();
        if ($this->_isbug == false) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchOne( $sql );
        return $data;
    }

    //这边查询我收到的求购报价

    public function getInquireQuoteCountByWhere($where){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ("iq_quotation","count(*)")
            ->joinLeft("iq_inquiry","iq_quotation.inquiry_id = iq_inquiry.id","")
            ->joinLeft ( "product", "iq_quotation.product_id = product.id", "")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "")
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "" )
            ->joinLeft ( "res_service", "res_service.id = iq_quotation.service_id", "" )
            ->joinLeft ( "res_currency", "res_currency.id = iq_quotation.currency_id", "")
            ->joinLeft ( "users_invoice as ui", "iq_quotation.business_id = ui.id","")
            ->where($where)->__toString ();

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchOne( $sql );
        return $data;
    }

    public function getInquireQuoteListByWhere($where,$item_page,$offset,$order,$group){
        $this->_table = new self ();
        $db = $this->_table->getAdapter ();
        $sql = $db->select ()->from ("iq_quotation")
            ->joinLeft("iq_inquiry","iq_quotation.inquiry_id = iq_inquiry.id","")
            ->joinLeft ( "product", "iq_quotation.product_id = product.id", "product_name")
            ->joinLeft ( "product_cates", "product.cate_id = product_cates.id", "cate_name")
            ->joinLeft ( "product_brands", "product.brand_id = product_brands.id", "brand_name" )
            ->joinLeft ( "res_service", "res_service.id = iq_quotation.service_id", "service_name" )
            ->joinLeft ( "res_currency", "res_currency.id = iq_quotation.currency_id", "mark as currency_mark")
            ->joinLeft ( "users_invoice as ui", "iq_quotation.business_id = ui.id",array("business_name","contacts","mobile","qq"))
            ->where($where)->__toString ();

        if($group){
            $sql.= " group  by ".$group;
        }
        if($order){
            $sql.= " order  by ".$order;
        }
        if($item_page){
            $sql.= " limit $offset,$item_page";
        }

        if ($this->_isbug == true) {
            echo  __METHOD__.": ".$sql . "<hr/>";
        }
        $data = $db->fetchAll( $sql );
        return $data;
    }
    //设置报价单取消

    public function setQuoteStatusByWhere($where,$data){
        $this->_table = new self();
        $db = $this->_table->getAdapter ();
        return $db->update("iq_quotation",$data,$where);
    }

}